package com.sos.core.game 
{
	
	import com.sos.core.Position;
	import com.sos.core.TargetType;
	import com.sos.utils.Utils;

	import flash.utils.Dictionary;

	public class GameActionTarget
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// Parameters
		public static const		PARAM_TARGET:String				= "target";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _type:TargetType;
		private var _params:Dictionary;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameActionTarget(type:TargetType)
		{
			// Init data
			_type = type;
			// Init objects
			_params = new Dictionary();
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get type():TargetType
		{
			return _type;
		}

		public function get params():Dictionary
		{
			return _params;
		}
		
		public function get hasParams():Boolean
		{
			return Utils.dictionarySize(_params) > 0;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		protected function validateParam(name:String, value:Object):Boolean
		{
			var valid:Boolean = false;
			// Check param name
			if (name == PARAM_TARGET)
			{
				// Type check on TargetType
				switch(_type)
				{
					case TargetType.NONE:
					{
						valid = false;
						break;
					}
					case TargetType.POSITION:
					{
						valid = value is Position;
					}
					case TargetType.EFFECT:
					{
						valid = value is GameMod;
					}
					case TargetType.GRAVEYARD:
					{
						valid = value is GameActionCard;
					}
				}
			}
			return valid;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function getParamValue(name:String):String
		{
			if (_params[name] != null)
			{
				// Type check on TargetType
				switch(_type)
				{
					case TargetType.NONE:		return null;
					case TargetType.POSITION:	return _params[name];
					case TargetType.EFFECT:		return _params[name];
					case TargetType.GRAVEYARD:	return _params[name];
					/*
					case TargetType.POSITION:	return (_params[name] as Position).sign;
					case TargetType.EFFECT:		return (_params[name] as GameMod).id.toString();
					case TargetType.GRAVEYARD:	return (_params[name] as GameActionCard).instanceId.toString();
					*/
				}
			}
			return null;
		}
		
		public function addParam(name:String, value:Object):void
		{
			//if (validateParam(name, value))
			if (true)
			{
				_params[name] = value;
			}
			else throw new Error("Param " + name + " of is not valid for TargetType " + _type);
		}
		
		public function removeParam(name:String):void
		{
			if (_params[name] != null)
			{
				delete _params[name];
			}
		}
		
		protected function clear():void
		{
			for (var name:String in _params)
			{
				delete _params[name];
			}
		}

	}
}